import 'package:get/get.dart';
import 'package:intl/intl.dart';
import 'package:share_fit/app/controllers/home/weather/model/weather_type.dart';
import 'package:share_fit/app/helper/ext/tr_ext.dart';
import 'package:share_fit/app/models/base_model.dart';
import 'package:share_fit/app/tools/date_tool.dart';
import 'package:share_fit/generated/locales.g.dart';

class DailyWeatherModel extends BaseModel {
  /// 预报日期
  DateTime? date;

  /// 日出时间，在高纬度地区可能为空
  String? sunrise;

  /// 日落时间，在高纬度地区可能为空
  String? sunset;

  /// 当天月升时间，可能为空
  String? moonrise;

  /// 当天月落时间，可能为空
  String? moonset;

  /// 月相名称
  String? moonPhase;

  /// 温度单位：0 -> 摄氏度，1 -> 华氏度
  int? tempUnit;

  /// 预报当天最高温度
  int? tempMax;

  /// 预报当天最低温度
  int? tempMin;

  /// 预报白天天气状况的图标代码，另请参考天气图标项目
  String? weatherMain;

  ///  预报白天天气状况文字描述，包括阴晴雨雪等天气状态的描述
  String? weatherDesc;

  /// 相对湿度，百分比数值
  double? humidity;

  /// 紫外线强度指数
  String? uvIndex;

  /// ⻛向⾓度0-360 int
  int? windDeg;

  /// ⻛速 double
  double? windSpeed;

  /// 降⽔概率，百分数1=100% double
  double? pop;

  DailyWeatherModel({
    this.date,
    this.tempUnit = 0,
    this.tempMax,
    this.tempMin,
    this.weatherMain,
    this.weatherDesc,
    this.humidity,
    this.windDeg,
    this.windSpeed,
    this.pop,
  });

  @override
  bool from(Map map) {
    try {
      date = DateTime.parse(map['date']);
      Map forcast = map['forcast'];
      tempMax = forcast['temp_max'].toInt();
      tempMin = forcast['temp_min'].toInt();
      weatherMain = forcast['weather_main'].toString();
      weatherDesc = forcast['weather_description'].toString();
      humidity = forcast['humidity'].toDouble();
      windDeg = forcast['wind_deg'].toInt();
      windSpeed = forcast['wind_speed'].toDouble();
      pop = forcast['pop'].toDouble();
    } catch (e) {
      return false;
    }
    return true;
  }

  @override
  toMap() {
    return {
      'timestamp': DateTool.getTimeInSeconds(date!),
      'phenomenon': WeatherType.getType(weatherDesc!),
      'highestTemperature': tempMax!,
      'lowestTemperature': tempMin!,
      // 'sunriseTime': (DateTime.parse('$fxDate $sunrise').microsecondsSinceEpoch / 1000000).truncate(),
      // 'sunsetTime': (DateTime.parse('$fxDate $sunset').microsecondsSinceEpoch / 1000000).truncate(),
      // 'moonRiseTime': (DateTime.parse('$fxDate $moonrise').microsecondsSinceEpoch / 1000000).truncate(),
      // 'moonSetTime': (DateTime.parse('$fxDate $moonset').microsecondsSinceEpoch / 1000000).truncate(),
      // 'moonPhase': MoonPhaseType.getType(moonPhase!),
      'hum': humidity!.toInt(),
      'uv': int.parse(uvIndex ?? '0'),
    };
  }

  @override
  List<Object?> get props => [date, tempMax, tempMin, weatherMain, weatherDesc, humidity, windDeg, windSpeed, pop];

  String _getTemperature(int temp) {
    if (tempUnit == 1) {
      double f = 32 + temp * 1.8;
      return '${f.round()}${LocaleKeys.temperature_unit_f.ts}';
    } else {
      return '$temp${LocaleKeys.temperature_unit_c.ts}';
    }
  }

  get icon => weatherDesc == null ? '' : 'assets/images/home/weather/${WeatherType.getWeatherIcon(weatherDesc)}';

  /// 获取当前温度
  get currentTemp => tempMin == null ? '' : _getTemperature(tempMin ?? 0);

  get tempRangeText {
    if (tempMin == null || tempMax == null) {
      return '';
    }
    return '${_getTemperature(tempMin!)} ~ ${_getTemperature(tempMax!)}';
  }

  get dateWeekText {
    if (date == null) return '';
    String week = DateFormat('E', Get.locale?.languageCode).format(date!);
    return '${DateFormat.yMMMMd(Get.locale?.languageCode).format(date!)}  $week';
  }

  get weekDateText {
    if (date == null) return '';
    String week = DateFormat('E', Get.locale?.languageCode).format(date!);
    String dateTemp = DateFormat.MMMd(Get.locale?.languageCode).format(date!);
    return ' $week  $dateTemp';
  }
}
